/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 22 mars 2011, 18:12:57
 */

package isi.corba.gui;

import isi.corba.idl.Enchere.Client;
import isi.corba.idl.Enchere.ClientHelper;
import isi.corba.idl.Enchere.GestClient;
import isi.corba.idl.Enchere.GestClientHelper;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.InvalidName;
import org.omg.CosNaming.NamingContextPackage.NotFound;

/**
 *
 * @author jof
 */
public class MainFrame extends javax.swing.JFrame {

    NamingContext ncRef=null;

    /** Creates new form MainFrame */
    public MainFrame(NamingContext ncRef) {
        this.ncRef=ncRef;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        panneauOngletPrincipal = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        etqSeConnecter = new javax.swing.JLabel();
        etqSeConnecterLogin = new javax.swing.JLabel();
        chSeConnecterLogin = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        panneauBtnSeConnecter = new javax.swing.JPanel();
        btnSeConnecter = new javax.swing.JButton();
        btnSeConnecterAnnuler = new javax.swing.JButton();
        chSeConnecterMotPasse = new javax.swing.JPasswordField();
        etqSeConnecterResultat = new javax.swing.JLabel();
        panneauCreerUtilisateur = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        chNom = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        chPrenom = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        chAdresse = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        chEmail = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        chLogin = new javax.swing.JTextField();
        panneauBtn = new javax.swing.JPanel();
        btnCreer = new javax.swing.JButton();
        btnAnnuler = new javax.swing.JButton();
        etqRetour = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        etqConnexion = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("[Aillères-Clavel] Enchères-CORBA");

        etqSeConnecter.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etqSeConnecter.setText("Connectez-vous!");

        etqSeConnecterLogin.setText("Votre login :");

        jLabel9.setText("Votre mot de passe :");

        btnSeConnecter.setText("Se connecter");
        btnSeConnecter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeConnecterActionPerformed(evt);
            }
        });
        panneauBtnSeConnecter.add(btnSeConnecter);

        btnSeConnecterAnnuler.setText("Annuler");
        panneauBtnSeConnecter.add(btnSeConnecterAnnuler);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(etqSeConnecterResultat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addComponent(panneauBtnSeConnecter, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addComponent(etqSeConnecter, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .addComponent(etqSeConnecterLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(chSeConnecterMotPasse)
                            .addComponent(chSeConnecterLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(etqSeConnecter)
                .addGap(39, 39, 39)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(etqSeConnecterLogin)
                    .addComponent(chSeConnecterLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(chSeConnecterMotPasse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(61, 61, 61)
                .addComponent(etqSeConnecterResultat, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(panneauBtnSeConnecter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panneauOngletPrincipal.addTab("Se Connecter", jPanel3);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Création d'un utilisateur");

        jLabel4.setText("Nom:");

        jLabel5.setText("Prenom:");

        jLabel6.setText("Adresse :");

        jLabel7.setText("Email:");

        jLabel8.setText("Login:");

        btnCreer.setText("Créer");
        btnCreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreerActionPerformed(evt);
            }
        });
        panneauBtn.add(btnCreer);

        btnAnnuler.setText("Annuler");
        panneauBtn.add(btnAnnuler);

        etqRetour.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout panneauCreerUtilisateurLayout = new javax.swing.GroupLayout(panneauCreerUtilisateur);
        panneauCreerUtilisateur.setLayout(panneauCreerUtilisateurLayout);
        panneauCreerUtilisateurLayout.setHorizontalGroup(
            panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panneauCreerUtilisateurLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panneauCreerUtilisateurLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(70, 70, 70))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panneauCreerUtilisateurLayout.createSequentialGroup()
                        .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addComponent(chAdresse, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addComponent(chNom, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addComponent(chPrenom, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addComponent(chEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(panneauCreerUtilisateurLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panneauBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(panneauCreerUtilisateurLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(etqRetour, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                .addContainerGap())
        );
        panneauCreerUtilisateurLayout.setVerticalGroup(
            panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panneauCreerUtilisateurLayout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(32, 32, 32)
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(chNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(chPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(chAdresse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panneauCreerUtilisateurLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(chLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(etqRetour, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panneauBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        panneauOngletPrincipal.addTab("Créer utilisateur", panneauCreerUtilisateur);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Enchères CORBA");

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Le seul site d'enchère avec un client lourd!");

        etqConnexion.setFont(new java.awt.Font("Dialog", 2, 12)); // NOI18N
        etqConnexion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etqConnexion.setText("Vous n'êtes pas connecté.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
            .addComponent(panneauOngletPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(etqConnexion, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(etqConnexion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addComponent(panneauOngletPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreerActionPerformed
         // resolve the Object Reference in Naming
            NameComponent nc = new NameComponent("Enchere", "");
            NameComponent path[] = {nc};
            
        try {
            if(ncRef==null) {
                System.out.println("Erreur namming context null");
            } else {

                GestClient clientRef = GestClientHelper.narrow(ncRef.resolve(path));
                // call the Hello server object and print results
                Client cltCree=clientRef.creer(chLogin.getText(), chNom.getText(), chPrenom.getText(), chEmail.getText(), chAdresse.getText());

                etqRetour.setForeground(Color.GREEN);
                etqRetour.setText("Client " + cltCree.nom + " créé avec succès. Mot de passe : " + cltCree.motPasse);
            }
        } catch (Exception e) { /* Une erreur c'est produite lors de la création du client */
            etqRetour.setForeground(Color.red);
            etqRetour.setText("La création du client a échoué");
            System.out.println("Exception lors de la création du client " + chNom.getText() + chPrenom.getText() + " :");
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_btnCreerActionPerformed

    private void btnSeConnecterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeConnecterActionPerformed
        // resolve the Object Reference in Naming
        NameComponent nc = new NameComponent("Enchere", "");
        NameComponent path[] = {nc};

        try {
            if(ncRef==null) {
                System.out.println("Erreur namming context null");
            } else {

                GestClient clientRef = GestClientHelper.narrow(ncRef.resolve(path));
                // call the Hello server object and print results
                Client cltLog=clientRef.logger(chSeConnecterLogin.getText(), chSeConnecterMotPasse.getText());

                if(cltLog!=null && cltLog.id>0) {
                    etqSeConnecterResultat.setForeground(Color.GREEN);
                    etqSeConnecterResultat.setText("Bienvenue " + cltLog.nom + " " + cltLog.prenom + ", vous êtes connecté(e)");
                    etqConnexion.setText("Bienvenue " + cltLog.nom + " " + cltLog.prenom + " et bonnes enchères...");
                    panneauOngletPrincipal.setEnabledAt(0, false);
                    btnSeConnecter.setEnabled(false);
                    btnSeConnecterAnnuler.setEnabled(false);
                    
                } else {
                    etqSeConnecterResultat.setForeground(Color.RED);
                    etqSeConnecterResultat.setText("Couple mot de passe/client (" + chSeConnecterLogin.getText() + ") inconnu(e)");
                }
                
            }
        } catch (Exception e) { /* Une erreur c'est produite lors de la création du client */
            etqSeConnecterResultat.setForeground(Color.red);
            etqSeConnecterResultat.setText("La connexion du client a échoué (exception générée)");
            System.out.println("Exception lors de la connexion du client " + chSeConnecterLogin.getText() + " :");
            System.out.println(e.getMessage());
        }
        panneauOngletPrincipal.setEnabledAt(0, false);
    }//GEN-LAST:event_btnSeConnecterActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnnuler;
    private javax.swing.JButton btnCreer;
    private javax.swing.JButton btnSeConnecter;
    private javax.swing.JButton btnSeConnecterAnnuler;
    private javax.swing.JTextField chAdresse;
    private javax.swing.JTextField chEmail;
    private javax.swing.JTextField chLogin;
    private javax.swing.JTextField chNom;
    private javax.swing.JTextField chPrenom;
    private javax.swing.JTextField chSeConnecterLogin;
    private javax.swing.JPasswordField chSeConnecterMotPasse;
    private javax.swing.JLabel etqConnexion;
    private javax.swing.JLabel etqRetour;
    private javax.swing.JLabel etqSeConnecter;
    private javax.swing.JLabel etqSeConnecterLogin;
    private javax.swing.JLabel etqSeConnecterResultat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel panneauBtn;
    private javax.swing.JPanel panneauBtnSeConnecter;
    private javax.swing.JPanel panneauCreerUtilisateur;
    private javax.swing.JTabbedPane panneauOngletPrincipal;
    // End of variables declaration//GEN-END:variables

}
